//
//  MyMessageListVC.m
//  CenturyCity
//
//  Created by beisheng03 on 2018/1/8.
//  Copyright © 2018年 beisheng. All rights reserved.
//

#import "MyMessageListVC.h"
#import "SysMessageModel.h"
#import "MessageListTabCell.h"
//#import "PulishRentInfoVC.h"
@interface MyMessageListVC ()
@property (nonatomic, assign) NSInteger curpage;

@property (nonatomic, assign) NSString *pageCount;

@property (nonatomic, assign) NSString *totalCount;

@property (nonatomic, strong) NSMutableArray<SysMessageModel *> *dataModels;
@end

@implementation MyMessageListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"消息";
    
    self.groupTableView.backgroundColor = viewBgGrey;
    
    
    self.groupTableView.rowHeight = UITableViewAutomaticDimension;
    
    self.groupTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.groupTableView.estimatedRowHeight = 800;
    

    
    [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([MessageListTabCell class]) bundle:nil] forCellReuseIdentifier:[MessageListTabCell identifierStr]];
    
    
    
    MJWeakSelf
    self.groupTableView.mj_header = [XMGRefreshHeader headerWithRefreshingBlock:^{
        
        [weakSelf loadData];
        
    }];
    
    [weakSelf.groupTableView.mj_header beginRefreshing];
    
    
    self.groupTableView.mj_footer = [XMGRefreshFooter footerWithRefreshingBlock:^{
        
        [weakSelf loadMoreData];
        
    }];
    
    
    
}



- (void)loadData{
    
    self.curpage = 1;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"page"] = @"1";
    parameters[@"pagesize"] = @"20";
    
    if (myViewModel.userIsLogin) {
        parameters[@"uid"] = myViewModel.user.uid;
    }
    
    
    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:GET_METHOD URLPath:@"Member/jpushLists" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            MYJSONSTR
            //            _model.lastString
            
            //           [[responseObject[@"data"][@"house"] firstObject]  propertyCode];
            
            weakSelf.pageCount = responseObject[@"data"][@"page_count"];
            
            weakSelf.totalCount = responseObject[@"data"][@"total_count"];
            
            
            weakSelf.dataModels = [SysMessageModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]];
            
            
        
            
            [weakSelf.groupTableView.mj_footer resetNoMoreData];
            
            if ([weakSelf.totalCount isEqualToString:@"0"]) {
                
                
            } else {
                
                if ([weakSelf.pageCount isEqualToString:@"1"]) {
                    
                    [weakSelf.groupTableView.mj_footer endRefreshingWithNoMoreData];
                    
                }
            }
            
            [weakSelf.groupTableView.mj_header endRefreshing];
            
            
            
            [weakSelf.groupTableView reloadData];
            
            
        } else {
            
            [weakSelf.groupTableView.mj_header endRefreshing];
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
    }];
    
    
    
    
    
}






- (void)loadMoreData{
    
    
    self.curpage ++;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"pagesize"] = @"20";
//    parameters[@"uid"] = myViewModel.user.uid;
    parameters[@"page"] = [NSString stringWithFormat:@"%ld",(long)self.curpage];
    if (myViewModel.userIsLogin) {
        parameters[@"uid"] = myViewModel.user.uid;
    }
    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools] sendRequestWithHttpMethod:GET_METHOD URLPath:@"Member/jpushLists" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            
            
            NSMutableArray<SysMessageModel *> *temp = [SysMessageModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]];
            
            
            

            
            [weakSelf.dataModels addObjectsFromArray:temp];

            
            
            
            
            if (weakSelf.pageCount.integerValue == self.curpage) {
                
                [weakSelf.groupTableView.mj_footer endRefreshingWithNoMoreData];
                
            } else {
                
                [weakSelf.groupTableView.mj_footer endRefreshing];
                
                
            }
            
            
            [weakSelf.groupTableView reloadData];
            
        }else{
            
            [weakSelf.groupTableView.mj_footer endRefreshing];
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
        
    }];
    
    
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (self.dataModels == nil) {
        return 0;
        
    } else {
        return 1;
        
    }
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    if (self.dataModels == nil) {
        
        return 0;
        
    } else {
        return self.dataModels.count;
        
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    MessageListTabCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MessageListTabCell identifierStr]];
    
    tableCell.model = self.dataModels[indexPath.row];
    
    return tableCell;
    
    
}





- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.00001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
        return 0.00001;

    
    
}



- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    
    UIView *temp = [[UIView alloc]init];
    temp.backgroundColor = [UIColor whiteColor];
    return temp;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *temp = [[UIView alloc]init];
    temp.backgroundColor = viewBgGrey;
    return temp;
    
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
@end
